import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class ButtonPanel extends JPanel{
	
	CardClient main;
	
	JButton btnAuction1=new JButton("1分");
 	JButton btnAuction2=new JButton("2分");
 	JButton btnAuction3=new JButton("3分");
 	JButton btnAuction0=new JButton("不叫");
 	
 	JButton btnPlay=new JButton("出牌");
	JButton btnPass=new JButton("不出");
	JButton btnAuto=new JButton("提示");
	//JButton btnAutoPlay=new JButton("托管");
	
    public ButtonPanel(CardClient main) {
    	super();
    	this.main=main;
    	init();
    }
            
    public void init(){
      	
      	ActionListener auctionAction=new ActionListener() {
      		public void actionPerformed(ActionEvent e) {
        		String txt=((JButton)e.getSource()).getText();
        		int bid=0;
        		if(txt.equalsIgnoreCase("1分")) bid=1;
        		if(txt.equalsIgnoreCase("2分")) bid=2;
        		if(txt.equalsIgnoreCase("3分")) bid=3;
        		if(txt.equalsIgnoreCase("不叫")) bid=0;
        		txt="cmd="+Connection.CMD_AUCTION
        			+"&ID="+main.player.id
        			+"&bid="+bid;
        		main.player.conn.sendMsg(txt);
        		main.player.setState(Player.WAIT_OTHER);
        		checkState();	
      		}
       	};
      	
      	btnAuction1.addActionListener(auctionAction);
       	btnAuction2.addActionListener(auctionAction);
       	btnAuction3.addActionListener(auctionAction);
       	btnAuction0.addActionListener(auctionAction);
       	
    	this.add(btnAuction1);
    	this.add(btnAuction2);
    	this.add(btnAuction3);
    	this.add(btnAuction0);
    	
    	ActionListener playActionListener=new ActionListener() {
      		public void actionPerformed(ActionEvent e) {
           	int [] cards=main.cardPanel.getSelectedCards();
           	if (cards==null) return;
           	if(isLegalComb(cards)) playCards(cards);
           	}
       	};

    	btnPlay.addActionListener(playActionListener);
    	
    	ActionListener passActionListener=new ActionListener() {
      		public void actionPerformed(ActionEvent e) {
           	pass();
           	}
       	};
       	
       	btnPass.addActionListener(passActionListener);
       	
       	ActionListener autoSelectActionListener=new ActionListener() {
      		public void actionPerformed(ActionEvent e) {
           	main.autoSelect();
           	}
       	};
       	
       	btnAuto.addActionListener(autoSelectActionListener);
       	
       	//ActionListener autoPlayActionListener=new ActionListener(){
       	//	public void actionPerformed(ActionEvent e) {
        //   	main.player.autoPlay=true;
        //   	}
       //	};
       	
       	//btnAutoPlay.addActionListener(autoPlayActionListener);
    	this.add(btnPlay);
    	this.add(btnPass);
    	this.add(btnAuto);
    	//this.add(btnAutoPlay);
    	
    	reset();
    }
    
    public void paintComponent(Graphics g) {
		Graphics2D comp = (Graphics2D)g;
		comp.setColor(main.getContentPane().getBackground());
		comp.fillRect(0,0,this.getSize().width,getSize().height);	
	}
	
	public void checkState(){
		reset();
	   	if(main.table.gameState==Table.AUCTION&&main.player.state== Player.THINKING){
			switch(main.table.bid){
			case 0:btnAuction1.setEnabled(true);
			case 1:btnAuction2.setEnabled(true);
    		case 2:btnAuction3.setEnabled(true);
    		case 3:btnAuction0.setEnabled(true);
			}
			btnAuction1.setVisible(true);
	   		btnAuction2.setVisible(true);
	   		btnAuction3.setVisible(true);
	   		btnAuction0.setVisible(true);
		}
		if((main.table.gameState==Table.PLAY||main.table.gameState==Table.NEWLOOP)&&main.player.state== Player.THINKING){
			btnPlay.setVisible(true);
			btnPass.setVisible(true);
			btnAuto.setVisible(true);
			if(main.table.gameState==Table.NEWLOOP) btnPass.setEnabled(false);
			}
		repaint();
	}
	
	private void reset(){
		btnAuction1.setVisible(false);
    	btnAuction2.setVisible(false);
    	btnAuction3.setVisible(false);
    	btnAuction0.setVisible(false);
    	btnAuction0.setEnabled(false);
    	btnAuction1.setEnabled(false);
    	btnAuction2.setEnabled(false);
    	btnAuction3.setEnabled(false);
    	
    	btnPlay.setVisible(false);
    	btnPass.setVisible(false);
    	btnPass.setEnabled(true);
    	btnAuto.setVisible(false);
	}
	
	//判断出的牌是否合法
	public boolean isLegalComb(int[] cards){
		CardType cardType=Rule.getType(cards);
		if(main.table.gameState==Table.NEWLOOP){
			if(cardType.type!=Rule.ILLEGAL) return true;
			else return false;
		}
		else if(Rule.compare(cards,main.table.getBigCards())==1) return true;
		else return false;
	}
	
	public void playCards(int [] cards){
		String msg;
		msg="cmd="+Connection.CMD_PLAY
			+"&ID="+main.player.id
			+"&seat="+main.player.seatNum
			+"&cards=";
		for(int i=0;i<cards.length;i++){
			msg+=cards[i]+",";
		}
		//System.out.println(msg);
		main.player.conn.sendMsg(msg);
		main.player.setState(Player.WAIT_OTHER);
		main.cardPanel.refresh();
	}
	
	public void pass(){
		int [] newCards={0};
		playCards(newCards);
	}
    
}